package leetcode;

import java.util.*;

public class code47 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = scanner.nextInt();
        }
        System.out.println(permute(array));

    }


    static List<List<Integer>> res = new ArrayList<>();

    static HashSet hashSet = new HashSet();

    static HashMap<Integer, Integer> map = new HashMap<>();

    public static List<List<Integer>> permute(int[] nums) {
        if (nums.length == 0) {
            return res;
        }
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        backtrack(nums, new ArrayList<>());
        return res;
    }

    private static void backtrack(int[] nums, List<Integer> list) {
        if (list.size() == nums.length) {
            String string = list.toString();
            if (!hashSet.contains(string)){
                hashSet.add(string);
                res.add(new ArrayList<>(list));
            }
        }
        else {
            for (int i = 0; i < nums.length; i++) {
                if (map.get(nums[i]) == 0) {
                    continue;
                }
                list.add(nums[i]);
                map.put(nums[i], map.get(nums[i]) - 1);
                backtrack(nums, list);
                list.remove(list.size() - 1);
                map.put(nums[i], map.get(nums[i]) + 1);
            }
        }
    }
}
